<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD><TITLE>lset manual page - Tcl Built-In Commands</TITLE>
<link rel="stylesheet" href="../docs.css" type="text/css" media="all">
</HEAD>
<BODY><H2><a href="../contents.htm">Tcl8.6.11/Tk8.6.11 Documentation</a> <small>&gt;</small> <a href="contents.htm">Tcl Commands</a> <small>&gt;</small> lset</H2>
<H3><A HREF="../UserCmd/contents.htm">Tcl/Tk Applications</A> | <A HREF="../TclCmd/contents.htm">Tcl Commands</A> | <A HREF="../TkCmd/contents.htm">Tk Commands</A> | <A HREF="../ItclCmd/contents.htm">[incr Tcl] Package Commands</A> | <A HREF="../SqliteCmd/contents.htm">SQLite3 Package Commands</A> | <A HREF="../TdbcCmd/contents.htm">TDBC Package Commands</A> | <A HREF="../TdbcmysqlCmd/contents.htm">tdbc::mysql Package Commands</A> | <A HREF="../TdbcodbcCmd/contents.htm">tdbc::odbc Package Commands</A> | <A HREF="../TdbcpostgresCmd/contents.htm">tdbc::postgres Package Commands</A> | <A HREF="../TdbcsqliteCmd/contents.htm">tdbc::sqlite3 Package Commands</A> | <A HREF="../ThreadCmd/contents.htm">Thread Package Commands</A> | <A HREF="../TclLib/contents.htm">Tcl C API</A> | <A HREF="../TkLib/contents.htm">Tk C API</A> | <A HREF="../ItclLib/contents.htm">[incr Tcl] Package C API</A> | <A HREF="../TdbcLib/contents.htm">TDBC Package C API</A></H3>
<H3><A NAME="M2">NAME</A></H3>
lset &mdash; Change an element in a list
<H3><A NAME="M3">SYNOPSIS</A></H3>
<B>lset </B><I>varName ?index ...? newValue</I><BR>
<H3><A NAME="M4">DESCRIPTION</A></H3>
The <B>lset</B> command accepts a parameter, <I>varName</I>, which
it interprets as the name of a variable containing a Tcl list.
It also accepts zero or more <I>indices</I> into
the list.  The indices may be presented either consecutively on the
command line, or grouped in a
Tcl list and presented as a single argument.
Finally, it accepts a new value for an element of <I>varName</I>.
<P>
If no indices are presented, the command takes the form:
<P>
<PRE><B>lset</B> varName newValue</PRE>
<P>
or
<P>
<PRE><B>lset</B> varName {} newValue</PRE>
<P>
In this case, <I>newValue</I> replaces the old value of the variable
<I>varName</I>.
<P>
When presented with a single index, the <B>lset</B> command
treats the content of the <I>varName</I> variable as a Tcl list.
It addresses the <I>index</I>'th element in it
(0 refers to the first element of the list).
When interpreting the list, <B>lset</B> observes the same rules
concerning braces and quotes and backslashes as the Tcl command
interpreter; however, variable
substitution and command substitution do not occur.
The command constructs a new list in which the designated element is
replaced with <I>newValue</I>.  This new list is stored in the
variable <I>varName</I>, and is also the return value from the <B>lset</B>
command.
<P>
If <I>index</I> is negative or greater than the number
of elements in <I>$varName</I>, then an error occurs.
<P>
If <I>index</I> is equal to the number of elements in <I>$varName</I>,
then the given element is appended to the list.
<P>
The interpretation of each simple <I>index</I> value is the same as
for the command <B><A HREF="../TclCmd/string.htm">string index</A></B>, supporting simple index
arithmetic and indices relative to the end of the list.
<P>
If additional <I>index</I> arguments are supplied, then each argument is
used in turn to address an element within a sublist designated
by the previous indexing operation,
allowing the script to alter elements in sublists (or append elements
to sublists).  The command,
<P>
<PRE><B>lset</B> a 1 2 newValue</PRE>
<P>
or
<P>
<PRE><B>lset</B> a {1 2} newValue</PRE>
<P>
replaces element 2 of sublist 1 with <I>newValue</I>.
<P>
The integer appearing in each <I>index</I> argument must be greater
than or equal to zero.  The integer appearing in each <I>index</I>
argument must be less than or equal to the length of the corresponding
list.  In other words, the <B>lset</B> command can change the size
of a list only by appending an element (setting the one after the current
end).  If an index is outside the permitted range, an error is reported.
<H3><A NAME="M5">EXAMPLES</A></H3>
In each of these examples, the initial value of <I>x</I> is:
<P>
<PRE>set x [list [list a b c] [list d e f] [list g h i]]
      <I><font size="+1">&rarr;</font> {a b c} {d e f} {g h i}</I></PRE>
<P>
The indicated return value also becomes the new value of <I>x</I>
(except in the last case, which is an error which leaves the value of
<I>x</I> unchanged.)
<P>
<PRE><B>lset</B> x {j k l}
      <I><font size="+1">&rarr;</font> j k l</I>
<B>lset</B> x {} {j k l}
      <I><font size="+1">&rarr;</font> j k l</I>
<B>lset</B> x 0 j
      <I><font size="+1">&rarr;</font> j {d e f} {g h i}</I>
<B>lset</B> x 2 j
      <I><font size="+1">&rarr;</font> {a b c} {d e f} j</I>
<B>lset</B> x end j
      <I><font size="+1">&rarr;</font> {a b c} {d e f} j</I>
<B>lset</B> x end-1 j
      <I><font size="+1">&rarr;</font> {a b c} j {g h i}</I>
<B>lset</B> x 2 1 j
      <I><font size="+1">&rarr;</font> {a b c} {d e f} {g j i}</I>
<B>lset</B> x {2 1} j
      <I><font size="+1">&rarr;</font> {a b c} {d e f} {g j i}</I>
<B>lset</B> x {2 3} j
      <I><font size="+1">&rarr;</font> list index out of range</I></PRE>
<P>
In the following examples, the initial value of <I>x</I> is:
<P>
<PRE>set x [list [list [list a b] [list c d]] &#92;
            [list [list e f] [list g h]]]
      <I><font size="+1">&rarr;</font> {{a b} {c d}} {{e f} {g h}}</I></PRE>
<P>
The indicated return value also becomes the new value of <I>x</I>.
<P>
<PRE><B>lset</B> x 1 1 0 j
      <I><font size="+1">&rarr;</font> {{a b} {c d}} {{e f} {j h}}</I>
<B>lset</B> x {1 1 0} j
      <I><font size="+1">&rarr;</font> {{a b} {c d}} {{e f} {j h}}</I></PRE>
<H3><A NAME="M6">SEE ALSO</A></H3>
<B><A HREF="../TclCmd/list.htm">list</A></B>, <B><A HREF="../TclCmd/lappend.htm">lappend</A></B>, <B><A HREF="../TclCmd/lindex.htm">lindex</A></B>, <B><A HREF="../TclCmd/linsert.htm">linsert</A></B>, <B><A HREF="../TclCmd/llength.htm">llength</A></B>, <B><A HREF="../TclCmd/lsearch.htm">lsearch</A></B>, <B><A HREF="../TclCmd/lsort.htm">lsort</A></B>, <B><A HREF="../TclCmd/lrange.htm">lrange</A></B>, <B><A HREF="../TclCmd/lreplace.htm">lreplace</A></B>, <B><A HREF="../TclCmd/string.htm">string</A></B>
<H3><A NAME="M7">KEYWORDS</A></H3>
<A href="../Keywords/E.htm#element">element</A>, <A href="../Keywords/I.htm#index">index</A>, <A href="../Keywords/L.htm#list">list</A>, <A href="../Keywords/R.htm#replace">replace</A>, <A href="../Keywords/S.htm#set">set</A>
<div class="copy">Copyright &copy; 2001 Kevin B. Kenny &lt;kennykb(at)acm.org&gt;. All rights reserved.
</div>
</BODY></HTML>
